import allure
import pytest


class CheckPoint:
    def __init__(self, driver):
        self.driver = driver
        self.flag = 0

    def __enter__(self):
        return self

    def __get_locate_by(self, actual_result_locate_by):
        if actual_result_locate_by:
            if actual_result_locate_by.lower() == 'xpath':
                return 'xpath'
            elif actual_result_locate_by.lower() == 'id':
                return 'id'
            elif actual_result_locate_by.lower() == 'link_text':
                return 'link_text'
            elif actual_result_locate_by.lower() == 'partial_link_text':
                return 'partial_link_text'
            elif actual_result_locate_by.lower() == 'tag_name':
                return 'tag_name'
            elif actual_result_locate_by.lower() == 'class_name':
                return 'class_name'
            elif actual_result_locate_by.lower() == 'css':
                return 'css selector'
        else:
            raise Exception("定位方式为空")

    @allure.step("数据检查")
    def check(self, check_by, expect_result, actual_result_locate_by, actual_result_locate_value, FLAG="page"):
        """
        输入内容包含检查方式为等于，用来检查预期结果和实际结果值是否一致
        :param check_by:
        :param expect_result:
        :param actual_result_locate_by:
        :param actual_result_locate_value:
        :return:
        """
        global actual_result
        if actual_result_locate_by and actual_result_locate_value:
            actual_result = self.driver.find_element(self.__get_locate_by(actual_result_locate_by),
                                                     value=actual_result_locate_value).text
        else:
            if FLAG == "page":
                actual_result = self.driver.page_source
            elif FLAG == "alert":
                Alert = self.driver.switch_to.alert
                actual_result = Alert.text
                Alert.accept()

        if check_by.strip() == '等于':
            try:
                assert expect_result == actual_result
                allure.attach(
                    f'断言失败：预期结果 [actual_result 包含 expect_result],实际结果：expect_result={expect_result},actual_result={actual_result},'
                    "正确信息比较结果", allure.attachment_type.TEXT)
            except:
                allure.attach(
                    f'断言失败：预期结果 [actual_result 包含 expect_result],实际结果：expect_result={expect_result},actual_result={actual_result},'
                    "错误信息比较结果", allure.attachment_type.TEXT)
                html = '<div><img src="data:img/png;base64,%s" alt="screenshot" style="width:600px;height:300px;"' \
                       'onclick="window.open(this.scr)" align="right"/></div>' % self.driver.get_screenshot_as_base64()
                allure.attach(html, "错误截屏", allure.attachment_type.HTML)

                self.flag += 1
        elif check_by.strip() == "包含":
            try:
                assert expect_result in actual_result
                allure.attach(
                    f'断言失败：预期结果 [actual_result 包含 expect_result],实际结果：expect_result={expect_result},actual_result={actual_result},'
                    "正确信息比较结果", allure.attachment_type.TEXT)
            except:
                allure.attach(
                    f'断言失败：预期结果 [actual_result 包含 expect_result],实际结果：expect_result={expect_result},actual_result={actual_result},'
                    "错误信息比较结果", allure.attachment_type.TEXT)
                html = '<div><img src="data:img/png;base64,%s" alt="screenshot" style="width:600px;height:300px;"' \
                       'onclick="window.open(this.scr)" align="right"/></div>' % self.driver.get_screenshot_as_base64()
                allure.attach(html, "错误截屏", allure.attachment_type.HTML)

                self.flag += 1

        else:
            try:
                assert False, f"输入的判断方式有误不是等于或者包含，实际值为{check_by}"
            except:
                self.flag += 1

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.flag > 0:
            assert False, "由于检查点错误，用例失败"
        else:
            pass
